package com.aizuda.boot.modules.business.contractNumber.controller;

import com.aizuda.boot.modules.business.contract.dto.SingleResponse;
import com.aizuda.boot.modules.business.contractNumber.domain.response.ContractNumberRuleDetailVO;
import com.aizuda.boot.modules.business.contractNumber.domain.response.ContractNumberRuleListVO;
import com.aizuda.boot.modules.business.contractNumber.domain.resquest.ContractNumberRuleSaveDTO;
import com.aizuda.boot.modules.business.contractNumber.domain.resquest.GenerateNumberDTO;
import com.aizuda.boot.modules.business.contractNumber.service.ContractNumberRuleService;
import com.aizuda.boot.modules.system.entity.SysDict;
import com.aizuda.boot.modules.system.service.ISysDictService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author sangyirong
 * @ClassName ContractNumberController
 * @Version 1.0
 * @Date 2025/6/27 17:33
 */
@RestController
@RequestMapping("/contractNumber")
@Tag(name = "合同编号管理", description = "合同编号管理")
public class ContractNumberController {

    private static final String dictCode = "autoNumber";

    @Resource
    private ISysDictService sysDictService;

    @Resource
    private ContractNumberRuleService contractNumberRuleService;


    @Operation(summary = "获取自动编号状态")
    @GetMapping("/getAutoNumber")
    public SingleResponse<Integer> getBackStatus() {
        SysDict auto = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery()
                .eq(SysDict::getCode, dictCode)
                .last("limit 1"));
        return SingleResponse.of(auto.getStatus());
    }

    @Operation(summary = "修改自动编号状态")
    @PutMapping("/updateAutoNumber")
    public SingleResponse updateBackStatus() {
        SysDict auto = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery()
                .eq(SysDict::getCode, dictCode)
                .last("limit 1"));
        auto.setStatus(auto.getStatus() == 1 ? 0 : 1);
        sysDictService.updateById(auto);
        return SingleResponse.success();
    }

    @Operation(summary = "创建合同编号规则")
    @PostMapping("create")
    public boolean createContractNumberRule(@RequestBody ContractNumberRuleSaveDTO dto) {
        return contractNumberRuleService.create(dto);
    }

    @Operation(summary = "获取合同编号规则列表")
    @GetMapping("list")
    public List<ContractNumberRuleListVO> list() {
        return contractNumberRuleService.getList();
    }

    @Operation(summary = "获取合同编号规则详情")
    @GetMapping("detail/{ruleId}")
    public ContractNumberRuleDetailVO detail(@PathVariable Long ruleId) {
        return contractNumberRuleService.getDetail(ruleId);
    }

    @Operation(summary = "修改合同编号规则")
    @PutMapping("update")
    public boolean update(@RequestBody ContractNumberRuleSaveDTO dto) {
        return contractNumberRuleService.updateRule(dto);
    }


    @Operation(summary = "删除合同编号规则")
    @DeleteMapping("delete/{ruleId}")
    public boolean delete(@PathVariable Long ruleId) {
        return contractNumberRuleService.removeRule(ruleId);
    }

    @Operation(summary = "生成合同编号")
    @PostMapping("getContractNumber")
    public String getContractNumber(@RequestBody GenerateNumberDTO dto) {
        return contractNumberRuleService.getContractNumber(dto);
    }
}
